#!/bin/bash

# 清屏
clear

# 提示用户输入文件名
echo "请输入要检测状态的文件名: "
read filename

# 初始显示文件状态或文件不存在错误
if [ -e "$filename" ] 
then
    initial_status=$(ls -l "$filename")
    echo "初始文件状态: "
    echo "$initial_status"
    initial_size=$(echo "$initial_status" | awk '{print $5}')
    change_count=0
    no_change_count=0
else
    echo "文件不存在!"
    exit 1
fi

while true
do
    sleep 5
    current_status=$(ls -l "$filename")
    current_size=$(echo "$current_status" | awk '{print $5}')
    if [ "$current_size" -eq "$initial_size" ] 
    then
        no_change_count=$((no_change_count + 1))
        if [ $no_change_count -eq 10 ]
        then
            echo "文件已连续检测10次大小未改变, 程序结束."
            # clear
            break
        fi
    else
        initial_size=$current_size
        change_count=$((change_count + 1))
        if [ $change_count -eq 2 ]
        then
            echo "文件已累计改变两次大小, 程序结束."
            # clear
            break
        fi
        echo "file [ $filename ] size changed to [ $current_size ]"
    fi
done